package Client;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

import javax.swing.JOptionPane;

import GroupClass.ConnectRMI;
import Project_etow.EtowInterface;

public class EditClient extends javax.swing.JDialog {
    
    private String checksex;
    private String id;
    private static String fullname;
    private static String brith;
    private static String sex;
    private static String company;
    private static String address1;
    private static String phone1;
    private static String email1;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    ConnectRMI connectRMI = new ConnectRMI();
    private javax.swing.JButton    btcancel;
    private javax.swing.JButton    btsubmit;
    private javax.swing.JComboBox  cbbgender;
    private javax.swing.JLabel     lbaddress;
    private javax.swing.JLabel     lbbrithday;
    private javax.swing.JLabel     lbcompany;
    private javax.swing.JLabel     lbemail;
    private javax.swing.JLabel     lbfullname;
    private javax.swing.JLabel     lbgender;
    private javax.swing.JLabel     lbphone;
    private javax.swing.JTextField txtaddress;
    private javax.swing.JTextField txtbrithday;
    private javax.swing.JTextField txtcompany;
    private javax.swing.JTextField txtemail;
    private javax.swing.JTextField txtfullname;
    private javax.swing.JTextField txtphone;

    public EditClient(java.awt.Frame parent, boolean modal, String name, String companay, String gender,
            String brithday, String address, String phone, String email) {
        super(parent, modal);
        fullname = name;
        company = companay;
        sex = gender;
        brith = brithday;
        address1 = address;
        phone1 = phone;
        email1 = email;
        initComponents();

    }

    @SuppressWarnings("unchecked")
    private void initComponents() {

        lbfullname = new javax.swing.JLabel();
        lbcompany = new javax.swing.JLabel();
        lbgender = new javax.swing.JLabel();
        lbbrithday = new javax.swing.JLabel();
        lbaddress = new javax.swing.JLabel();
        lbphone = new javax.swing.JLabel();
        lbemail = new javax.swing.JLabel();
        txtfullname = new javax.swing.JTextField();
        txtcompany = new javax.swing.JTextField();
        txtbrithday = new javax.swing.JTextField();
        txtaddress = new javax.swing.JTextField();
        txtphone = new javax.swing.JTextField();
        txtemail = new javax.swing.JTextField();
        cbbgender = new javax.swing.JComboBox();
        btsubmit = new javax.swing.JButton();
        btcancel = new javax.swing.JButton();
        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        lbfullname.setText("FullName");

        lbcompany.setText("Company");

        lbgender.setText("Gender");

        lbbrithday.setText("Brithday");

        lbaddress.setText("Address");

        lbphone.setText("Phone");

        lbemail.setText("Email");

        cbbgender.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Male", "Female" }));

        txtfullname.setText(fullname);
        txtbrithday.setText(brith);
        cbbgender.setSelectedItem(sex);
        txtcompany.setText(company);
        txtaddress.setText(address1);
        txtphone.setText(phone1);
        txtemail.setText(email1);

        btsubmit.setText("Submit");
        btsubmit.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                try {
                    Registry registry = LocateRegistry.getRegistry(connectRMI.host, connectRMI.port);
                    EtowInterface obj = (EtowInterface) registry.lookup(connectRMI.regis);
                    String name = txtfullname.getText();
                    String brithday = txtbrithday.getText();
                    String sex = Integer.toString(cbbgender.getSelectedIndex());
                    if (sex.equals("1") == true){
                        checksex =  "Female";
                    }
                    else {
                        checksex =  "Male";
                    }
                    String company = txtcompany.getText();
                    String address = txtaddress.getText();
                    String email = txtemail.getText();
                    int phone = Integer.parseInt(txtphone.getText());
                    id = getId();
                    
                    String result1 =  obj.Edit(name, company, checksex, brithday, address, phone, email, Integer.parseInt(id));
                    if (result1.equals("ok")) {
                        JOptionPane.showMessageDialog(null, "Update Successful");
                        setVisible(false);
                        
                    } else {
                        JOptionPane.showMessageDialog(null, "Update Fail");
                    }
                } catch (Exception ex) {

                }

            }
        });
        btcancel.setText("Cancel");
        btcancel.addActionListener(new ActionListener() {
            
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println(id);
 
                
            }
        });
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(layout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(
                        layout.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(
                                        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                .addGroup(
                                                        layout.createSequentialGroup()
                                                                .addComponent(lbfullname)
                                                                .addPreferredGap(
                                                                        javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                                                .addComponent(txtfullname,
                                                                        javax.swing.GroupLayout.DEFAULT_SIZE, 199,
                                                                        Short.MAX_VALUE))
                                                .addGroup(
                                                        layout.createSequentialGroup()
                                                                .addGroup(
                                                                        layout.createParallelGroup(
                                                                                javax.swing.GroupLayout.Alignment.LEADING)
                                                                                .addComponent(lbcompany)
                                                                                .addComponent(lbgender))
                                                                .addPreferredGap(
                                                                        javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                                                .addGroup(
                                                                        layout.createParallelGroup(
                                                                                javax.swing.GroupLayout.Alignment.LEADING)
                                                                                .addGroup(
                                                                                        layout.createSequentialGroup()
                                                                                                .addComponent(
                                                                                                        cbbgender,
                                                                                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                                                                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                                                                                .addPreferredGap(
                                                                                                        javax.swing.LayoutStyle.ComponentPlacement.RELATED,
                                                                                                        141,
                                                                                                        Short.MAX_VALUE))
                                                                                .addComponent(
                                                                                        txtcompany,
                                                                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                                        197, Short.MAX_VALUE)))
                                                .addGroup(
                                                        layout.createSequentialGroup()
                                                                .addGroup(
                                                                        layout.createParallelGroup(
                                                                                javax.swing.GroupLayout.Alignment.LEADING)
                                                                                .addComponent(lbaddress)
                                                                                .addComponent(lbphone)
                                                                                .addComponent(lbemail))
                                                                .addGap(18, 18, 18)
                                                                .addGroup(
                                                                        layout.createParallelGroup(
                                                                                javax.swing.GroupLayout.Alignment.LEADING)
                                                                                .addGroup(
                                                                                        layout.createSequentialGroup()
                                                                                                .addComponent(btsubmit)
                                                                                                .addPreferredGap(
                                                                                                        javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                                                                .addComponent(btcancel))
                                                                                .addComponent(
                                                                                        txtaddress,
                                                                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                                        195, Short.MAX_VALUE)
                                                                                .addComponent(
                                                                                        txtphone,
                                                                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                                        195, Short.MAX_VALUE)
                                                                                .addComponent(
                                                                                        txtemail,
                                                                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                                        195, Short.MAX_VALUE)))
                                                .addGroup(
                                                        layout.createSequentialGroup()
                                                                .addComponent(lbbrithday)
                                                                .addGap(18, 18, 18)
                                                                .addComponent(txtbrithday,
                                                                        javax.swing.GroupLayout.DEFAULT_SIZE, 194,
                                                                        Short.MAX_VALUE))).addGap(43, 43, 43)));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
                layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(
                                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(lbfullname)
                                        .addComponent(txtfullname, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(
                                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(lbcompany)
                                        .addComponent(txtcompany, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(24, 24, 24)
                        .addGroup(
                                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(lbgender)
                                        .addComponent(cbbgender, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(
                                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(lbbrithday)
                                        .addComponent(txtbrithday, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(
                                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(lbaddress)
                                        .addComponent(txtaddress, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(
                                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(lbphone)
                                        .addComponent(txtphone, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(
                                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                        .addComponent(lbemail)
                                        .addComponent(txtemail, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(
                                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(btsubmit).addComponent(btcancel))
                        .addContainerGap(26, Short.MAX_VALUE)));

        pack();
    }

    public static void main(String args[]) {

        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(EditClient.class.getName())
                    .log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(EditClient.class.getName())
                    .log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(EditClient.class.getName())
                    .log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(EditClient.class.getName())
                    .log(java.util.logging.Level.SEVERE, null, ex);
        }

        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                EditClient dialog = new EditClient(new javax.swing.JFrame(), true, fullname, company, sex, brith, address1, phone1, email1);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
}
